package com.excellent.system.controller;

import com.excellent.common.annotation.Log;
import com.excellent.common.core.controller.BaseController;
import com.excellent.common.core.domain.AjaxResult;
import com.excellent.common.core.domain.entity.SysUser;
import com.excellent.common.core.page.TableDataInfo;
import com.excellent.common.enums.BusinessType;
import com.excellent.common.utils.ShiroUtils;
import com.excellent.common.utils.poi.ExcelUtil;
import com.excellent.system.domain.SysTaskStu;
import com.excellent.system.service.ISysTaskStuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Calendar;
import java.util.Collections;
import java.util.List;

/**
 * 负责课题Controller
 *
 * @author ruoyi
 * @date 2022-05-02
 */
@Controller
@RequestMapping("/system/teatask")
public class SysTaskStuTeaController extends BaseController
{
    private String prefix = "system/teatask";

    @Autowired
    private ISysTaskStuService sysTaskStuService;

    @GetMapping()
    public String teatask()
    {
        return prefix + "/teatask";
    }

    /**
     * 查询负责课题列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysTaskStu sysTaskStu)
    {
        SysUser user = ShiroUtils.getSysUser();
        if(user.isAdmin()){   //如果当前是管理员登录显示所有的数据
            if (sysTaskStu.getTaskTerm().equals("")) {
                Calendar c = Calendar.getInstance();  //获取系统当前年份
                int month = c.get(Calendar.MONTH) + 1;//本函数中月份需要加一
                int year = c.get(Calendar.YEAR);
                if (month >= 1 && month <= 6) {
                    sysTaskStu.setTaskTerm((year-1)+"-"+(year) + "学年春季学期");
                } else {
                    sysTaskStu.setTaskTerm((year)+"-"+(year + 1) + "学年秋季学期");
                }
                startPage();
                List<SysTaskStu> list = sysTaskStuService.selectSysTaskStuList(sysTaskStu);
                Collections.sort(list);
                return getDataTable(list);
            } else {
                startPage();
                List<SysTaskStu> list = sysTaskStuService.selectSysTaskStuList(sysTaskStu);
                Collections.sort(list);
                return getDataTable(list);
            }
        }
        else {  //如果是普通教师只查询和自己双选的同学
            sysTaskStu.setTeaId(Long.parseLong(user.getLoginName()));
            if (sysTaskStu.getTaskTerm().equals("")) {
                Calendar c = Calendar.getInstance();  //获取系统当前年份
                int month = c.get(Calendar.MONTH) + 1;//本函数中月份需要加一
                int year = c.get(Calendar.YEAR);
                if (month >= 1 && month <= 6) {
                    sysTaskStu.setTaskTerm((year-1)+"-"+(year) + "学年春季学期");
                } else {
                    sysTaskStu.setTaskTerm((year)+"-"+(year + 1) + "学年秋季学期");
                }
                startPage();
                List<SysTaskStu> list = sysTaskStuService.selectSysTaskStuList(sysTaskStu);
                Collections.sort(list);
                return getDataTable(list);
            } else {
                startPage();
                List<SysTaskStu> list = sysTaskStuService.selectSysTaskStuList(sysTaskStu);
                Collections.sort(list);
                return getDataTable(list);
            }
        }
    }

    /**
     * 导出负责课题列表
     */
    @Log(title = "负责课题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysTaskStu sysTaskStu)
    {
        List<SysTaskStu> list = sysTaskStuService.selectSysTaskStuList(sysTaskStu);
        ExcelUtil<SysTaskStu> util = new ExcelUtil<SysTaskStu>(SysTaskStu.class);
        return util.exportExcel(list, "teatask");
    }

    /**
     * 新增负责课题
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存负责课题
     */
    @Log(title = "负责课题", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysTaskStu sysTaskStu)
    {
        return toAjax(sysTaskStuService.insertSysTaskStu(sysTaskStu));
    }

    /**
     * 修改负责课题
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysTaskStu sysTaskStu = sysTaskStuService.selectSysTaskStuById(id);
        mmap.put("sysTaskStu", sysTaskStu);
        return prefix + "/edit";
    }

    @GetMapping("/edit1/{id}")
    public String edit1(@PathVariable("id") Long id, ModelMap mmap) {
        SysTaskStu sysTaskStu = sysTaskStuService.selectSysTaskStuById(id);
        mmap.put("sysTaskStu", sysTaskStu);
        return prefix + "/edit1";
    }
    /**
     * 修改保存负责课题
     */
    @Log(title = "负责课题", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysTaskStu sysTaskStu)
    {
        return toAjax(sysTaskStuService.updateSysTaskStu(sysTaskStu));
    }

    /**
     * 删除负责课题
     */
    @Log(title = "负责课题", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysTaskStuService.deleteSysTaskStuByIds(ids));
    }

    /***
     *
     * 教师进行保存后无法进行修改
     */
    @PostMapping("/keep")
    @ResponseBody
    public AjaxResult keep(int id)
    {

        String s = id+"";
        SysTaskStu sysTaskStu = new SysTaskStu(); //对当前保存状态进行保存
        sysTaskStu.setId(Long.parseLong(s));
        sysTaskStu.setScoreStatus(1);
        sysTaskStuService.updateSysTaskStu(sysTaskStu);
        return toAjax(true);
    }
}
